const Service = require('egg').Service;
var fs       = require('fs');
const common = require('../common/common')

const puppeteer = require('puppeteer');

class JiaoFei180Service extends Service {
    async doService(params) {
        let browser = null;
        let page = null;
        if(!global.bowerauto || !global.bowerauto.browser){
            global.bowerauto = {};
            browser = await puppeteer.launch({
                headless: false,
                ignoreDefaultArgs: ["--enable-automation"]
              } );

            page = await browser.newPage();

            global.bowerauto.browser = browser;
            global.bowerauto.page = page;
        }else{
            browser = global.bowerauto.browser;
            page = global.bowerauto.page;
        }
        
          try{
              await page.goto("https://coupon.jf180.cn/fx?c=2531391968884262");

          }catch(e){
            global.bowerauto = {};
            browser = await puppeteer.launch({
                headless: false,
                ignoreDefaultArgs: ["--enable-automation"]
              } );

            page = await browser.newPage();

            global.bowerauto.browser = browser;
            global.bowerauto.page = page;
          }
          await page.waitFor('#btn-active');
          await page.type('#phone',"13401977114");
          page.click('#btn-active');
          await common.timeout(3000);
          let url = page.url();
          if(url.indexOf("login.m.taobao.com")!=-1){
              console.log("enter taobao login");
              await page.type('#username',"13401977114" ,{delay: 100});
              await page.type('#password',"198975Cq" ,{delay: 100});
              page.click('#btn-submit');
              await common.timeout(3000);
              let SM_TXT_1 = await page.$("#SM_TXT_1");
              if(SM_TXT_1!=null){
                  console.log("需要验证");

                  page.click('.km-dialog-buttons .km-dialog-btn',{delay:100});
                  page.click('.sm-ico');
                  await common.timeout(6000);
                //   await page.type('#username',"13401977114");
                  await page.type('#password',"198975Cq" ,{delay: 100});
                  page.click('#btn-submit');
                  await common.timeout(3000);
                  let km_dialog_btn = await page.$(".km-dialog-btn");
                  if(km_dialog_btn!=null){
                    page.click('.km-dialog-buttons .km-dialog-btn' ,{delay:100});
                    await common.timeout(2000);
                    page.click('.sm-ico' ,{delay:100});
                    await common.timeout(6000);
                  //   await page.type('#username',"13401977114");
                    await page.type('#password',"198975Cq" ,{delay: 100});
                    page.click('#btn-submit');
                    await common.timeout(2000);
                    await page.type('#password',"198975Cq" ,{delay: 100});
                  }
              }
          }else if(url.indexOf("https://h5.m.taobao.com/app/ucenter/www/index.html")!=-1){
            await page.type('.mod-input input',"13401977114");
          }
        return {code : "00" ,msg: "操作成功"};
    }
  }
  
  module.exports = JiaoFei180Service;